package com.tjsinfo.tjsoa.system.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.tjsinfo.tjsoa.common.DealString;
import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.reminded.bo.TSystemRemindedManager;
import com.tjsinfo.tjsoa.system.bo.IDepartmentBo;
import com.tjsinfo.tjsoa.system.bo.ISystemUserBo;
import com.tjsinfo.tjsoa.system.bo.ISystemUserDepartmentBo;
import com.tjsinfo.tjsoa.system.bo.ISystemYiZhangBO;
import com.tjsinfo.tjsoa.system.vo.TSystemDepartment;
import com.tjsinfo.tjsoa.system.vo.TSystemUserDepartment;
import com.tjsinfo.tjsoa.system.vo.TSystemYinzhang;

public class LoginAction extends Action {
	private ISystemUserBo systemUserBo;
	private ISystemUserDepartmentBo systemUserDepartmentBo;
	private IDepartmentBo departmentBo;
	private ISystemYiZhangBO systemYiZhangBO;
	private TSystemRemindedManager systemRemindedManager;
	
	
	public TSystemRemindedManager getRemindedManager() {
		return systemRemindedManager;
	}

	public void setRemindedManager(TSystemRemindedManager systemRemindedManager) {
		this.systemRemindedManager = systemRemindedManager;
	}

	public ISystemYiZhangBO getSystemYiZhangBO() {
		return systemYiZhangBO;
	}

	public void setSystemYiZhangBO(ISystemYiZhangBO systemYiZhangBO) {
		this.systemYiZhangBO = systemYiZhangBO;
	}

	public IDepartmentBo getDepartmentBo() {
		return departmentBo;
	}

	public void setDepartmentBo(IDepartmentBo departmentBo) {
		this.departmentBo = departmentBo;
	}

	public ISystemUserDepartmentBo getSystemUserDepartmentBo() {
		return systemUserDepartmentBo;
	}

	public void setSystemUserDepartmentBo(
			ISystemUserDepartmentBo systemUserDepartmentBo) {
		this.systemUserDepartmentBo = systemUserDepartmentBo;
	}

	public void setSystemUserBo(ISystemUserBo systemUserBo) {
		this.systemUserBo = systemUserBo;
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			boolean isUser = systemUserBo.isUser(request);
			if (isUser) {
				MessageResources message=getResources(request);
				String systemCommonUserRole=message.getMessage(getLocale(request),"system.common.userRole");
				Integer userIdInteger = ToolKit.getSessionUser(request).getUser_id();
				String userRole=systemUserBo.getUserRoles(userIdInteger);
				ToolKit.getSessionUser(request).setPermission(DealString.noAgain(systemCommonUserRole+userRole));
				//wjj add cookie
			    String savecookieString = request.getParameter("savecookie");
			    if ("true".equals(savecookieString))
			    {
					  Cookie newCookie = new Cookie("RQOAuserId",String.valueOf(userIdInteger));
			    	  newCookie.setMaxAge(30*24*3600);
			    	  newCookie.setPath("/");
			    	  response.addCookie(newCookie);
			    }else{
			    	  Cookie newCookie = new Cookie("RQOAuserId",null);
			    	  newCookie.setMaxAge(0);
			    	  newCookie.setPath("/");
			    	  response.addCookie(newCookie);
			    }
			    //wjj add end
				List<TSystemUserDepartment> systemuserdepartmentlist =  systemUserDepartmentBo.selectByUserId(ToolKit.getSessionUser(request).getUser_id());
				List<String> departmentlist = new ArrayList<String>();
				for (TSystemUserDepartment systemUserDepartment : systemuserdepartmentlist) {
					departmentlist.add(systemUserDepartment.getDepartmentId().trim());
				}
				ToolKit.getSessionUser(request).setDepartment(ToolKit.arrayToString(departmentlist.toArray(new String[]{})));
				for (TSystemUserDepartment systemUserDepartment : systemuserdepartmentlist) {
					String departmentId = systemUserDepartment.getDepartmentId();
					TSystemDepartment systemDepartment = departmentBo.findById(departmentId);
					if(systemDepartment.getIsZhiShu() != null){
						request.getSession().setAttribute("zhishu", "1");
					}else{
						request.getSession().setAttribute("zhishu", "0");
					}
				}
				List<TSystemYinzhang> systemYinzhangList = new ArrayList<TSystemYinzhang>(); 
				for (String departmentId : departmentlist) {
					 
					List<TSystemYinzhang> tempList =  systemYiZhangBO.selectYinZhangByDepartmentId(departmentId);
					 systemYinzhangList.addAll(tempList);
				}
				request.getSession().setAttribute("systemYinzhangList", systemYinzhangList);
				String tanchu = request.getParameter("tanchu");
				if("true".equals(tanchu)){
					return mapping.findForward("successLink");
				}
				//直接跳转到传入地址
				if(request.getParameter("loginUrl")!= null)
				{
					request.setAttribute("linkUrl", request.getParameter("loginUrl"));
					return mapping.findForward("Link");
				}else{
					return mapping.findForward("success");
				}
			} else {
				request.setAttribute("login", "un");
				return mapping.findForward("UnLogin");
			}
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("Exception", e);
			return mapping.findForward("failed");
		}
	}
}
